#nullable enable
namespace Application.Service.LogCache;

/// <summary>
/// 日志缓存提供器 - 拦截Furion日志并写入缓存
/// </summary>
public class LogCacheProvider : ILoggerProvider
{
    private readonly LogCacheService _logCacheService;

    public LogCacheProvider(LogCacheService logCacheService)
    {
        _logCacheService = logCacheService;
    }

    public ILogger CreateLogger(string categoryName)
    {
        return new LogCacheLogger(categoryName, _logCacheService);
    }

    public void Dispose()
    {
        // 清理资源
    }
}

/// <summary>
/// 日志缓存记录器
/// </summary>
public class LogCacheLogger : ILogger
{
    private readonly string _categoryName;
    private readonly LogCacheService _logCacheService;

    public LogCacheLogger(string categoryName, LogCacheService logCacheService)
    {
        _categoryName = categoryName;
        _logCacheService = logCacheService;
    }

    public IDisposable? BeginScope<TState>(TState state) where TState : notnull
    {
        return null;
    }

    public bool IsEnabled(Microsoft.Extensions.Logging.LogLevel logLevel)
    {
        return true;
    }

    public void Log<TState>(Microsoft.Extensions.Logging.LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
    {
        // 将Microsoft.Extensions.Logging的LogLevel转换为自定义LogLevel
        var customLogLevel = logLevel switch
        {
            Microsoft.Extensions.Logging.LogLevel.Trace => LogLevel.Trace,
            Microsoft.Extensions.Logging.LogLevel.Debug => LogLevel.Debug,
            Microsoft.Extensions.Logging.LogLevel.Information => LogLevel.Information,
            Microsoft.Extensions.Logging.LogLevel.Warning => LogLevel.Warning,
            Microsoft.Extensions.Logging.LogLevel.Error => LogLevel.Error,
            Microsoft.Extensions.Logging.LogLevel.Critical => LogLevel.Critical,
            Microsoft.Extensions.Logging.LogLevel.None => LogLevel.Information,
            _ => LogLevel.Information
        };

        var message = formatter(state, exception);
        
        // 创建日志条目
        var logEntry = new LogEntry
        {
            Level = customLogLevel,
            Message = message,
            Exception = exception,
            CallerMemberName = _categoryName
        };

        // 添加到缓存
        _logCacheService.AddLog(logEntry);
    }
}

/// <summary>
/// Furion日志拦截器 - 拦截Furion的静态日志调用
/// </summary>
public static class LogInterceptor
{
    private static LogCacheService? _logCacheService;

    /// <summary>
    /// 初始化日志拦截器
    /// </summary>
    /// <param name="logCacheService">日志缓存服务</param>
    public static void Initialize(LogCacheService logCacheService)
    {
        _logCacheService = logCacheService;
        
        // 重写Furion的静态日志方法
        OverrideFurionLogMethods();
    }

    /// <summary>
    /// 重写Furion的静态日志方法
    /// </summary>
    private static void OverrideFurionLogMethods()
    {
        // 由于Furion的Log类是静态的，我们需要通过反射或其他方式拦截
        // 这里我们创建一个包装器来拦截日志调用
    }

    /// <summary>
    /// 拦截Furion的日志写入
    /// </summary>
    public static void InterceptLog(LogLevel level, string message, Exception? exception = null, 
        [CallerMemberName] string memberName = "",
        [CallerFilePath] string filePath = "",
        [CallerLineNumber] int lineNumber = 0)
    {
        if (_logCacheService != null)
        {
            var logEntry = new LogEntry
            {
                Level = level,
                Message = message,
                Exception = exception,
                CallerMemberName = memberName,
                CallerFilePath = filePath,
                CallerLineNumber = lineNumber
            };

            _logCacheService.AddLog(logEntry);
        }
    }
}
